package com.lhx.wechat.utils;


import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;


/**
 * 在本项目中使用的发起http请求工具
 * SIMPLE HTTP CLIENT 工具类
 * 
 */
public class CustomerSimpleHttpClient {

    private static final Logger logger = LoggerFactory.getLogger(CustomerSimpleHttpClient.class);

    private static final String DEFAULT_CHARSET = "UTF-8";

    private int timeout = 20;

    private int poolSize = 20;

    /**
     * 初始化CloseableHttpClient
     */
    private CloseableHttpClient init() {
        // --SET TIMEOUT--//
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(timeout * 1000)
                .setConnectTimeout(timeout * 1000)
                .setConnectionRequestTimeout(timeout * 1000)
                .setStaleConnectionCheckEnabled(true)
                .build();

        // --SET POOLSIZE, CREATE HTTP CLIENT--//
        return HttpClientBuilder.create().setMaxConnTotal(poolSize).setMaxConnPerRoute(poolSize)
                .setDefaultRequestConfig(requestConfig).build();
    }

    /**
     * POST请求
     *
     * @param url          URL地址
     * @param requestParam 字符串形式的请求参数
     * @param charset      编码
     * @return response
     */
    public String postWithStringParam(String url, String requestParam, String charset) {
        CloseableHttpClient httpClient = init();
        HttpPost httpPost = new HttpPost(url);
        String body = null;

        try {
            httpPost.setEntity(new StringEntity(requestParam, charset));

            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity entity = httpResponse.getEntity();

            body = EntityUtils.toString(entity);

            EntityUtils.consume(entity);
        } catch (IOException e) {
            httpPost.abort();
            logger.error("post from remote " + url + "  error", e);
        } finally {
            destroy(httpClient);
        }
        return body;
    }

    public String post(String url, String param) {
        return postWithStringParam(url, param, DEFAULT_CHARSET);
    }



    public String get(String url, String charset) {
        CloseableHttpClient httpClient = init();
        HttpGet httpGet = null;
        String body = null;

        //RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig).build();
        try {
            //创建HTTP GET请求
            httpGet = new HttpGet(url);
            //System.out.println("executing request " + httpGet.getURI());

            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

            //获得响应体
            HttpEntity entity = httpResponse.getEntity();

            //这两行代码等同于下面的toString方法
            /*
            byte[] bytes = EntityUtils.toByteArray(entity);
            
            body = new String(bytes, DEFAULT_CHARSET);
            */

            body = EntityUtils.toString(entity);

            if (entity != null) {
                EntityUtils.consume(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            httpGet.abort();
            logger.error("get from remote " + url + "  error", e);
        } finally {
            destroy(httpClient);
        }
        return body;
    }

    public String get(String url) {
        return get(url, DEFAULT_CHARSET);
    }


    /**
     * 关闭CloseableHttpClient Instance
     */
    public void destroy(CloseableHttpClient httpClient) {
        try {
            httpClient.close();
        } catch (IOException e) {
            logger.error("httpclient close fail", e);
        }
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    public int getPoolSize() {
        return poolSize;
    }

    public void setPoolSize(int poolSize) {
        this.poolSize = poolSize;
    }

}
